package com.qfedu.ms.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.ms.Vo.MessageVo;
import com.qfedu.ms.Vo.PageVo;
import com.qfedu.ms.Vo.TableVo;
import com.qfedu.ms.constants.Constants;
import com.qfedu.ms.dao.UserDao;
import com.qfedu.ms.entity.User;
import com.qfedu.ms.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@Configuration
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    /**
     * 登录验证
     * @param username
     * @param password
     * @return
     */
    @Override
    public User LoginCheck(String username, String password) {
        log.info("userService接收参数：{}，{}",username,password);
        User user = userDao.selectUserByUsername(username);
        log.info("useDao返回参数：{}",user);
        if (user != null){
            if (password.equals(user.getPassword())){
                log.info("useService返回参数：{}",user);
                return user;
            }else {
                return null;
            }
        }
        return null;
    }


    /**
     * 获取所有用户
     * @return
     */
    @Transactional(propagation =  Propagation.SUPPORTS)
    @Override
    public TableVo<User> getAllUser(PageVo pageVo) {

        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        List<User> list = userDao.selectAllUser();
        List<User> users = new ArrayList<>();
        for (User user : list){
            users.add(user);
        }
        PageInfo<User> pageInfo = new PageInfo<User>(users);

        return new TableVo<User>(0,"OK",pageInfo.getTotal(),users);
    }

    @Override
    public MessageVo registerUser(User user, MultipartFile source, String realPath) {
        log.info("userService接收参数：{},{},{}",user,source,realPath);

        //先检查用户名是否存在
        User user1 = userDao.selectUserByUsername(user.getUsername());
        if (user1 != null){
            //用户名已存在
            return new MessageVo(Constants.USER_CODE_RESIT_FAIL,Constants.USER_MSG_RESIT_FAIL,null);
        }


        try {
            //处理文件上传，把文件存放到指定目录
            //文件原始名称
            String filename = source.getOriginalFilename();

            //定制全局唯一的命名
            String unique = UUID.randomUUID().toString();

            //获得文件的后缀
            String ext = FilenameUtils.getExtension(filename);

            //定制全局唯一的文件名
            String uniFileName = unique + "." + ext;
            log.info("头像的文件名为:{}",uniFileName);

            File file = new File(realPath);
            if (!file.exists()){
                file.mkdirs();
            }

            source.transferTo(new File(realPath+"\\"+uniFileName));
            log.info("头像的存储路径为:{}",realPath+"\\"+uniFileName);
            String img_url = "/upload/" + uniFileName;

            user.setImg_url(img_url);
            user.setRegister_data(new Date());

            log.info("传入useDao参数:{}",user);
            userDao.insertUser(user);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return new MessageVo(Constants.USER_CODE_RESIT_SUCCESS,Constants.USER_MSG_RESIT_SUCCESS,null);
    }

    @Override
    public TableVo<User> getAllUser(PageVo pageVo, User user) {
        log.info("userService接收参数:{},{}",pageVo,user);
        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        List<User> list = userDao.selectAllUserByCondtion(user);
        List<User> users = new ArrayList<>();
        for (User user1 : list){
            users.add(user1);
        }
        PageInfo<User> pageInfo = new PageInfo<User>(users);
        TableVo<User> tableVo = new TableVo<>(0, "OK", pageInfo.getTotal(), users);
        log.info("userService返回参数:{}",tableVo);
        return tableVo;
    }
}
